﻿using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;
using Volo.Abp.ObjectMapping;
using Xms.Abp.Modularity;

namespace Xms.Abp.ObjectMapping;

[DependsOn(typeof(AbpAutoMapperModule))]
public class XmsAbpObjectMappingModule : XmsModule
{
    public override void PostConfigureServices(ServiceConfigurationContext context)
    {
        base.PostConfigureServices(context);

        var list = context.Services.Where(r => r.ServiceType == typeof(IObjectMapper<>) && r.ImplementationType == typeof(DefaultObjectMapper<>)).ToArray();
        foreach (var item in list)
        {
            context.Services.Remove(item);
        }
        context.Services.AddTransient(
            typeof(IXmsObjectMapper<>),
            typeof(XmsObjectMapper<>)
        );
        context.Services.AddTransient(
            typeof(IObjectMapper<>),
            typeof(XmsObjectMapper<>)
        );

        context.Services.AddXmsAutoMapperObjectMapper();

    }
}

